<template>
  <el-form :model="SmsLoginForm" :rules="rules2" ref="loginForm" label-position="left" label-width="0px"
           class="demo-ruleForm login-container">
    <h3 class="title">系统登录</h3>
    <el-form-item prop="account">
      <el-input type="text" v-model="SmsLoginForm.phone" auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" style="width:200px;" v-model="SmsLoginForm.smsCode" auto-complete="off"
                placeholder="验证码"></el-input>
      <button type="button" style="width:20%;" class="btn" @click="sendMobileCode">获取</button>
    </el-form-item>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:45%;" @click.native.prevent="accountLogin" :loading="logining">登录
      </el-button>
      <el-button type="primary" style="width:45%;" @click.native.prevent="gotoLogin" :loading="logining">密码登录
      </el-button>


      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>
  </el-form>
</template>

<script>
import {requestLogin} from '../api/api';
//import NProgress from 'nprogress'
export default {
  data() {
    return {
      logining: false,
      SmsLoginForm: {
        phone: '',
        smsCode: '',
        type: 0
      },
      rules2: {
        phone: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          //{ validator: validaePass }
        ],
        smsCode: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          //{ validator: validaePass2 }
        ]
      },
      checked: true
    };
  },
  methods: {
    // handleReset2() {
    //   this.$refs.ruleForm2.resetFields();
    // },
    sendMobileCode() {
      // 1.判断手机号不为空
      if (!this.SmsLoginForm.phone) {
        alert("手机号不能为空");
        return;
      }
      //5.发送ajax请求
      this.$http.post("/verifyCode/smsCode", this.SmsLoginForm)
          .then(result => {
            result = result.data;
            if (result.success) {
              this.$message({
                message: "验证码已经发送到您的手机，请在5分钟内使用",
                type: 'success'
              })
            }
          })
    },
    gotoRegister() {
      // 跳转店铺入驻页面
      this.$router.push({path: '/shopRegister'});
    },
    gotoLogin() {
      this.$router.push({path: '/login'});
    },
    accountLogin() {
      this.logining = true;
      this.$http.post("/login/smsCode", this.SmsLoginForm)
          .then(result => {
            this.logining = false;
            result = result.data;
            if (result.success) {
              this.$message({
                message: "登录成功",
                type: 'success'
              });
              // token logininfo 放入到localStorage中
              let {token, logininfo} = result.resultObj;
              localStorage.setItem("token", token);
              localStorage.setItem("logininfo", JSON.stringify(logininfo));
              // 跳转首页
              this.$router.push({path: '/echarts'});
            } else {
              this.$message({
                message: "登录失败",
                type: 'error'
              });
            }
          })
          .catch(result => {
            this.logining = false;
            this.$message({
              message: "网络信号不好",
              type: 'error'
            });
          })
    }
  }
}

</script>

<style lang="scss" scoped>
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;

  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }

  .remember {
    margin: 0px 0px 35px 0px;
  }
}
</style>